package org.developer.es.controller;

import org.developer.es.domain.entity.BlogModel;
import org.developer.es.domain.repository.BlogRepository;
import org.developer.es.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Optional;

/**
 * 参考网址：https://segmentfault.com/a/1190000018625101
 */

@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogRepository blogRepository;

    @PostMapping("/add")
    public ResponseEntity add(@RequestBody BlogModel blogModel){

        blogRepository.save(blogModel);
        return ResponseEntity.ok().body("添加成功");
    }

    @GetMapping("/get/{id}")
    public ResponseEntity getById(@PathVariable String id){

        Optional<BlogModel> blogModelOptional = blogRepository.findById(id);
        if(blogModelOptional.isPresent()){

            BlogModel blogModel = blogModelOptional.get();
            return ResponseEntity.ok().body(blogModel);
        }

        return null;
    }


}
